//
//  KKSearchVC.m
//  DoMi
//
//  Created by 康凯 on 2016/12/1.
//  Copyright © 2016年 kk. All rights reserved.
//

#import "KKSearchVC.h"
#import "KKSearchResultVC.h"
#import "KKHotSearchCell.h"
#import "KKStudyHttp.h"
#import "KKHotSearchM.h"
#define History @"search_history"
@interface KKSearchVC ()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITextField *searchContent;
@property (weak, nonatomic) IBOutlet UIView *deleteView;
@property (weak, nonatomic) IBOutlet UIView *historyView;
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (strong, nonatomic) NSArray *hotDatas;
@property (strong, nonatomic) NSUserDefaults *user;
@end

@implementation KKSearchVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setNavBlack];
    self.navigationItem.title=@"搜索";
    [self initView];
    [self getHotSearchList];
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    for (UIButton *btn in self.historyView.subviews) {
        [btn removeFromSuperview];
    }
    self.user=[NSUserDefaults standardUserDefaults];
    NSMutableArray *history=[self.user objectForKey:History];
    if(history.count>0){
        float perWidth=self.historyView.frame.size.width/4;
        for (int i=0; i<history.count; i++) {
            if(i<8){
                int perHeight=i/4;
                UIButton *historyItem=[[UIButton alloc] initWithFrame:CGRectMake(i%4*perWidth, perHeight*30, perWidth, 30)];
                historyItem.tag=i;
                historyItem.layer.shadowColor=[UIColor blackColor].CGColor;
                historyItem.layer.shadowOpacity=0.7;
                historyItem.layer.shadowOffset=CGSizeMake(0,0);
                [historyItem setTitle:history[i] forState:UIControlStateNormal];
                [self.historyView addSubview:historyItem];
            }
        }
    }
}
-(void)initView{
    //tableview
    self.tableView.delegate=self;
    self.tableView.dataSource=self;
    self.tableView.rowHeight=68;
    [self.tableView registerNib:[UINib nibWithNibName:@"KKHotSearchCell" bundle:nil] forCellReuseIdentifier:@"KKHotSearchCell"];
    
    UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(delHistory)];
    [self.deleteView addGestureRecognizer:tap];
}

#pragma mark tableview代理
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    KKHotSearchCell *search=(KKHotSearchCell *)cell;
    [search initData:self.hotDatas[indexPath.row] number:indexPath.row];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.hotDatas.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    return [tableView dequeueReusableCellWithIdentifier:@"KKHotSearchCell"];
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    KKHotSearchM *hot=self.hotDatas[indexPath.row];
    KKSearchResultVC *result=[[KKSearchResultVC alloc] init];
    result.searchContent=hot.title;
    [self.navigationController pushViewController:result animated:YES];
}

#pragma mark 按钮--删除历史记录
-(void)delHistory{
    [self.user removeObjectForKey:History];
    for (UIButton *btn in self.historyView.subviews) {
        [btn removeFromSuperview];
    }
}
#pragma mark 按钮--搜索
- (IBAction)searchClick:(id)sender {
    if([KKAppUtils isEmptyString:self.searchContent.text]){
        [self toast:@"请输入搜索内容"];
        return;
    }
    NSMutableArray *history=[self.user objectForKey:History];
    NSMutableArray *temp=[NSMutableArray array];
    if(history!=nil){
        [temp addObjectsFromArray:history];
    }
    [temp insertObject:self.searchContent.text atIndex:0];
    [self.user setObject:temp forKey:History];
    [self.user synchronize];
    KKSearchResultVC *result=[[KKSearchResultVC alloc] initWithNibName:@"KKSearchResultVC" bundle:nil];
    result.searchContent=self.searchContent.text;
    [self.navigationController pushViewController:result animated:YES];
    self.searchContent.text=@"";
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}
#pragma mark 接口--热门搜索
-(void)getHotSearchList{
    [self showDialog];
    [KKStudyHttp getHotSearchList:^(id data) {
        self.hotDatas=data;
        [self.tableView reloadData];
        [self dismissDialog];
    } failure:^(NSError *error) {
        [self dismissDialog];
    }];
}
@end
